﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.WorkFlow;
using Enterprise.Model.ZhiWu;
using Enterprise.Service.ZhiWu;
using Enterprise.Service.Department;
namespace Enterprise.Service.WorkFlow
{
    /// <summary>
    /// 流程引擎中心
    /// </summary>
    public class WorkFlowService
    {
        /// <summary>
        /// 流程遍历审核人员 返回审核人员列表
        /// </summary>
        /// <param name="wfId"></param>
        /// <param name="userId"></param>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public string GetWokrflowCheckUsers(int wfId, int userId, int deptId)
        {
            string reStr = "";
            WorkFlowNodeService nService = new WorkFlowNodeService();
            List<WorkFlowNodeEntity> list = nService.WorkFlowNode(wfId);
            if (list.Count > 0)
            {
                string Pre = "";
                for (int i = 0; i < list.Count; i++)
                {
                    string Now = GetCheckUserId(list[i].NOBJECTTYPE, list[i].NOBJECT, userId, deptId);
                    if (Now != Pre)
                    {
                        Pre = Now;
                        reStr += Now + ",";
                    }
                }
            }
            return reStr.TrimEnd(',');
        }

        /// <summary>
        /// 审核人员 返回审核人员
        /// </summary>
        /// <param name="objectType">流程节点办理类型  0 指定成员，1 指定职务，2 申请人的部门管理人、3 申请人的部门主管、4流程申请人</param>
        /// <param name="objectUser">流程节点办理人员 当objectType 取值为0 or 1时，获取值</param>
        /// <param name="userId">流程申请部门Id</param>
        /// <param name="deptId">流程申请部门Id</param>
        /// <returns></returns>
        private string GetCheckUserId(int objectType, int objectUser, int userId, int deptId)
        {
            string rInt = "";
            switch (objectType)
            {
                case 0:
                    rInt = objectUser.ToString();
                    break;
                case 1:
                    rInt = GetZhiWuUserId(objectUser).ToString();
                    break;
                case 2:
                    rInt = GetDeptManager(deptId).ToString();
                    break;
                case 3:
                    rInt = GetDeptHeaderManager(deptId).ToString();
                    break;
                case 4:
                    rInt = userId.ToString();
                    break;
            }
            return rInt;
        }

        /// <summary>
        ///  取得该职务的用户编号
        /// </summary>
        /// <param name="objectUser"></param>
        /// <returns></returns>
        private int GetZhiWuUserId(int objectUser)
        {
            UserZhiwuService uzService = new UserZhiwuService();
            var q = uzService.UserZhiWuList().Where(p => p.ZWID == objectUser).FirstOrDefault();
            if (q != null)
            {
                return q.USERID;
            }
            return 0;
        }

        /// <summary>
        /// 获取部门部门经理(主任)编码
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        private int GetDeptManager(int deptId)
        {
            DepartmentService dService = new DepartmentService();
            var q = dService.DepartmentDisp(deptId);
            if (q != null)
            {
                return q.DMANAGER;
            }
            return 0;
        }

        /// <summary>
        /// 获取部门分管领导编码
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        private int GetDeptHeaderManager(int deptId)
        {
            DepartmentService dService = new DepartmentService();
            var q = dService.DepartmentDisp(deptId);
            if (q != null)
            {
                return q.DHEADERMANAGER;
            }
            return 0;
        }

        /// <summary>
        /// 流程单当前审批状态
        /// </summary>
        /// <param name="workFlowId">流程单</param>
        /// <param name="iState">状态</param>
        /// <returns></returns>
        public static string GetWorkFlowState(string workFlowId,int iState)
        {
            string reStr = "";
            switch (iState)
            {
                case 1:
                    reStr = "<img src=\"/Styles/images/complete.png\" border=\"0\" alt=\"审批通过\" title=\"审批通过\"/>";
                    break;
                case 2:
                    reStr = "<img src=\"/Styles/images/stop.png\" border=\"0\" alt=\"审批未通过\" title=\"审批未通过\"/>";
                    break;
                case 0:
                    reStr = WorkFlowProcessService.WorkFlowProcessCurrentState(workFlowId, iState);
                    break;
            }
            return reStr;
        }

    }
}
